<template>
  <div class="app-container">
    <el-form
      :inline="true"
    >
      <el-form-item
        :label="$t('area.Layer')"
        prop="postName"
      >
        <treeselect
          v-model="mapId"
          size="small"
          :placeholder="$t('operating.pleaseChosen')"
          :show-count="true"
          :max-height="600"
          :options="mapTreeData"
          :default-expand-level="1"
          @input="nodeClick"
        />
      </el-form-item>
      <el-form-item
        :label="$t('history.date')"
        prop="postName"
      >
        <el-date-picker
          v-model="timeValue1"
          size="small"
          type="datetime"
          :placeholder="$t('operating.StartTime')"
          @change="change1"
        />
        -
        <el-date-picker
          v-model="timeValue2"
          size="small"
          type="datetime"
          :placeholder="$t('operating.EndTime')"
          @change="change2"
        />

      </el-form-item>
      <el-form-item :label="$t('history.deptname')">
        <!-- <treeselect
            v-model="deptValue"
            size="small"
            :placeholder="$t('operating.pleaseChosen')"
            :show-count="true"
            :max-height="600"
            :options="depts"
            :default-expand-level="1"
          /> -->
        <treeselect
          v-model="deptValue"
          :options="depts"
          :show-count="true"
          :normalizer="normalizer"
          :placeholder="$t('operating.pleaseChosen')"
          :default-expand-level="1"
          :max-height="600"
        />
      </el-form-item>
      <el-form-item
        :label="$t('staff.staffTypeName')"
        prop="postName"
      >
        <el-select
          v-model="resourceType"
          style="width: 100px;"
          size="small"
        >
          <el-option
            v-for="item in resourceTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item :label="$t('history.deptname')" prop="postCode">
        <treeselect
          v-model="queryParams.param.deptValue"
          :placeholder="$t('operating.pleaseChosen')"
          :show-count="true"
          :maxHeight="600"
          :options="depData"
          :default-expand-level="1"
          @input="nodeClick"
          style="width: 150px; margin-right: 10px"
        >
        </treeselect>
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="query"
        >{{ $t("operating.search") }}</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
            $t("operating.reset")
          }}</el-button> -->
      </el-form-item>
      <!-- <el-form-item>
          <el-slider v-model="value5"
                     :max="20"
                     :min="1"
                     @change="changeRadius"
                     style="width:200px;"></el-slider>
        </el-form-item> -->
    </el-form>

    <!-- <el-header>

        <div class="handle-box">
          <span>{{$t('area.Layer')}}:</span>
          <treeselect v-model="mapId"
                      :placeholder="$t('operating.pleaseChosen')"
                      :show-count="true"
                      :maxHeight="600"
                      :options="mapTreeData"
                      :default-expand-level="1"
                      @input="nodeClick">
          </treeselect>
          <span>{{$t('staff.staffTypeName')}}:</span>
          <el-select v-model="resourceType"
                     style="width: 100px;">
            <el-option v-for="item in resourceTypes"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
          <span>{{$t('history.deptname')}}:</span>
          <treeselect v-model="deptValue"
                      :placeholder="$t('operating.pleaseChosen')"
                      :show-count="true"
                      :maxHeight="600"
                      :options="depts"
                      :default-expand-level="1">
          </treeselect>
          <span>{{$t('history.date')}}:</span>
          <el-date-picker v-model="timeValue1"
                          type="datetime"
                          @change="change1"
                          :placeholder="$t('operating.StartTime')"
                          style="width:200px;">
          </el-date-picker>
          <el-date-picker v-model="timeValue2"
                          type="datetime"
                          @change="change2"
                          :placeholder="$t('operating.EndTime')"
                          style="width:200px;">
          </el-date-picker>
          <span>
            <el-button icon="el-icon-search"
                       @click="query"
                       style="background-color:#f1a11e; color: #fff;">{{ $t('operating.search')}}</el-button>
          </span>
          <span>
            {{$t('Notice.modificationOfThermalDiagram')}}
          </span>
          <span>
            <el-slider v-model="value5"
                       :max="20"
                       :min="1"
                       @change="changeRadius"
                       style="width:200px;"></el-slider>
          </span>
        </div>
      </el-header> -->
    <div class="context">
      <div id="map" />
    </div>

    <!-- 左下角坐标显示和缩放比例 -->
    <div class="positiondiv">
      <span />
    </div>
    <div class="scalediv">
      <span />
    </div>
  </div>
</template>

<script>
import i18n from '@/utils/lang/index'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {
  sendThis,
  Load_Map,
  _iMap,
  _TransPixel,
  TagStyle,
  _Style,
  updateOverlayPosition,
  FenceStyle,
  _TransMetric,
  _TransMetricFence,
  _dPR,
  Load_Fence
} from '@/utils/historyMap.js'
import {
  webSockUrl,
  webSockStatisticsUrl,
  cameraServerUrl,
  baseUrl,
  initPicPath,
  webSockMonitorUrl,
  rateHeartShow
} from '@/utils/global'
// import { Vector as VectorSource } from 'ol/source.js'
import { Vector as VectorLayer } from 'ol/layer.js'

import VectorSource from 'ol/source/Vector'
import { Heatmap as HeatmapLayer } from 'ol/layer'

import Feature from 'ol/Feature'
import Point from 'ol/geom/Point'
import { LineString, MultiLineString } from 'ol/geom.js'
import { Style, Stroke } from 'ol/style.js'
import KML from 'ol/format/KML'

import {
  getMapTree,
  getMap
} from '@/api/location'
import { listDept } from '@/api/system/dept'
import {
  getDeptTree,
  getPosDataEx
} from '@/api/system/historyDisplayInfo.js'

import { treeselect } from '@/api/system/dept'

export default {
  components: { Treeselect },
  data() {
    return {
      mapId: '',
      oldMapId: null,
      mapTreeData: [],
      radio: '1',
      timeValue1: new Date(new Date().getTime() - 1000 * 60 * 30),
      timeValue2: new Date(),
      lineSource: null,
      lineLayer: null,
      rloading: [],
      trackDatas: [],
      multipleSelection: [], // 表格中勾选的数据
      lineStyle: {
        'linestring': new Style({
          stroke: new Stroke({
            color: 'yellow',
            width: 1
          }),
          zIndex: 999
        })
      },
      deptValue: null,
      depts: [],
      resourceType: 'all',
      resourceTypes: [
        { value: 'all', label: this.$t('heatChart.all') },
        { value: 'staff', label: this.$t('realtime.staff') },
        { value: 'material', label: this.$t('realtime.material') }
      ],
      value5: 15
    }
  },
  mounted() {
    this.initData()
    this.getDeptList()
  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      }
    },
    initData() {
      // 加载地图树
      getMapTree().then((res) => {
        const RES = this.responseUse(res)
        if (typeof RES !== 'undefined') {
          this.mapTreeData = RES
          sendThis(this)
          this.mapId = RES[0].id
        }
      })
    },
    getDeptList() {
      listDept().then((res) => {
        const RES = this.responseUse(res)
        if (typeof RES !== 'undefined') {
          this.depts = this.handleTree(RES, 'deptId')
        }
      })
      // getDeptTree().then((res) => {
      //   const RES = this.responseUse(res)
      //   if (typeof RES !== 'undefined') {
      //     this.depts = RES
      //   }
      // })
    },
    nodeClick() {
      if (this.oldMapId == this.mapId) {
        this.$message({
          message: i18n.t('Notice.modificationOfThermalDiagram'),
          type: 'warning'
        })
      } else if (this.mapId !== undefined) {
        this.oldMapId = this.mapId
        getMap(this.mapId).then(res => {
          const RES = this.responseUse(res)
          if (RES != undefined) {
            $('#map').empty()
            Load_Map(baseUrl + '/file' + RES.mapPath, RES.mapActualX,
              RES.mapActualY,
              RES.mapPixelX,
              RES.mapPixelY,
              RES.mapPixelX / RES.mapActualX, [RES.mapOriginX, RES.mapOriginY], false)
            this.loadLayer()
          }
        })
      }
    },
    change1() {
      if (this.timeValue1 > this.timeValue2) {
        var tt = this.timeValue1.getTime()
        var ntt = tt + 1000 * 60 * 120
        this.timeValue2 = new Date(ntt)
      }
    },
    change2() {
      if (this.timeValue2 < this.timeValue1) {
        var tt = this.timeValue2.getTime()
        var ntt = tt - 1000 * 60 * 120
        this.timeValue1 = new Date(ntt)
      }
    },
    query() {
      this.trackDatas = []
      this.lineSource.clear(true)
      // this.rloading = this.$loading({
      //   lock: true,
      //   text: i18n.t('Notice.modificationOfThermalDiagram'),
      //   spinner: 'el-icon-loading',
      //   background: 'rgba(0,0,0,0.7)'
      // })
      // setTimeout(() => {
      //   this.rloading.close()
      // }, 1000 * 13)
      var param = {
        'pageNum': 1,
        'pageSize': 10,
        'param': {
          'layerId': parseInt(this.mapId),
          'resourceType': this.resourceType === 'all' ? '' : this.resourceType, // 资源类型
          'startTime': this.v_FormatDateTime(new Date(this.timeValue1).getTime()),
          'endTime': this.v_FormatDateTime(new Date(this.timeValue2).getTime()),
          'enableLog': true,
          'departmentId': this.deptValue
        }
      }
      getPosDataEx(param).then(res => {
        // this.rloading.close()
        const RES = this.responseUse(res)
        if (RES != undefined && RES != null) {
          this.trackDatas = RES
          var array = []
          for (var i = 0; i < this.trackDatas.length; i++) {
            const coordinate = [parseFloat(this.trackDatas[i].x), parseFloat(this.trackDatas[i].y)]
            const feature = new Feature({
              geometry: new Point(_TransPixel(coordinate)),
              weight: parseFloat(this.trackDatas[i].count) / parseFloat(1000)
            })
            array.push(feature)
          }
          this.lineSource.addFeatures(array)
        } else {
          this.$message({
            message: i18n.t('operating.switchQuery')
          })
        }
      })
    },
    // 加载地位图层和线图层,
    loadLayer() {
      this.lineSource = new VectorSource({
        wrapX: false
      })
      this.lineLayer = new HeatmapLayer({
        source: this.lineSource,
        blur: 7, // 模糊半径
        radius: this.value5// 半径大小
      })
      _iMap.addLayer(this.lineLayer)
    },
    changeRadius(val) {
      this.lineLayer.setRadius(parseInt(val, 10))
    }
  }
}
</script>

<style scoped>

.handle-box {
  min-width: 1125px;
}

.handle-box * {
  float: left;
}

.handle-box span {
  line-height: 28px;
  margin-left: 30px;
}

.context {
  padding: 0;
  width: 100%;
  height: calc(100% - 60px);
}
.context div {
  float: left;
}
#map {
  width: 100%;
  height: 100%;
}

>>> .vue-treeselect__control {
  width: 180px;
}

.infotable {
  position: absolute;
  width: 530px;
  top: 60px;
  left: 50px;
}

 /deep/ .el-button--mini.is-circle {
  width: 36px;
  height: 36px;
}

</style>
